package io.github.doc.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * <p>Company: 深圳振瀚信息技术有限公司成都分公司 </p>
 * <p>Description: </p>
 *
 * @author ZH.Team
 * @version 1.0.0
 * @email "mailto:ZH.Team@zhxx.com"
 * @date 2024.12.01 20:59
 * @since y.y.y
 */
@Slf4j
public class TemplateUtils {

    /**
     * 获取到模板的字符串
     *
     * @param templateName template name
     * @return the template
     * @since y.y.y
     */
    public static String getTemplateString(String templateName) {
        ClassLoader loader = TemplateUtils.class.getClassLoader();
        URL loaderResource = loader.getResource("template/" + templateName);
        if (loaderResource == null) {
            return StrUtil.EMPTY;
        }
        try {
            InputStream inputStream = loaderResource.openConnection().getInputStream();
            if (inputStream == null) {
                return StrUtil.EMPTY;
            }
            BufferedInputStream stream = new BufferedInputStream(inputStream);
            byte[] bytes = stream.readAllBytes();
            return new String(bytes);
        } catch (IOException e) {
            log.error("Can't find template " + templateName, e);
        }
        return StrUtil.EMPTY;
    }

}
